package com.longrise.bymodule.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/2/16.
 * 获取手机联系人
 */

public class PhoneContactHelper {

    private static final String TAG = "PhoneContactHelper";
    private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };

    public String getContact(Context context) {
        String contactResult = null;
        ContactBean contactBean = new ContactBean();
        //得到ContentResolver对象
        ContentResolver cr = context.getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        //向下移动光标
        while (cursor != null && cursor.moveToNext()) {
            //取得联系人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String contact = cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);

            ContactItem contactItem = new ContactItem();
            contactItem.name = contact;

            while (phone.moveToNext()) {
                String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                //格式化手机号
                PhoneNumber = PhoneNumber.replace("-", "");
                PhoneNumber = PhoneNumber.replace(" ", "");
                PhoneNumber = PhoneNumber.replace("+86", "");

                contactItem.phoneNum.add(PhoneNumber);
            }
            phone.close();
            if (contactItem.phoneNum.size() != 0) {
                contactBean.contacts.add(contactItem);
            }
        }
        cursor.close();
        Gson gson = new Gson();
        contactResult = gson.toJson(contactBean);
        return contactResult;
    }

    public class ContactBean {
        public List<ContactItem> contacts = new ArrayList<>();
    }

    public class ContactItem {
        public String name;
        public List<String> phoneNum = new ArrayList<>();
    }


    /**
     * 快速获取联系人 但是多个电话的联系人 是分开取出来的 而且无顺序
     * @param context
     */
    public String getContactsFast(Context context){
        String contactResult = null;
        Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
        ContactBean contactBean = new ContactBean();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String contactName = cursor.getString(0);
                String phoneNumber = cursor.getString(1);
                phoneNumber = phoneNumber.replace("-", "");
                phoneNumber = phoneNumber.replace(" ", "");
                phoneNumber = phoneNumber.replace("+86", "");
                ContactItem contactItem = new ContactItem();
                contactItem.name = contactName;
                contactItem.phoneNum.add(phoneNumber);
                if (contactItem.phoneNum.size() != 0) {
                    contactBean.contacts.add(contactItem);
                }
            }
            cursor.close();
        }
        Gson gson = new Gson();
        contactResult = gson.toJson(contactBean);
        return contactResult;
    }
}
